package aishua.com.mytestapplist.datastructure;

import android.annotation.TargetApi;
import android.os.Build;
import android.support.v4.app.INotificationSideChannel;

import java.sql.Array;
import java.util.Arrays;

/**
 * Created by aishua on 2018/4/25.
 */

public class SequenceStack<T> {

    private static final String TAG = "SequenceStack--";
    //默认数组大小
    private int DEFAULT_SIZE = 10;
    //    数组保存数据元素
    private Object[] element;
    //    数组的长度
    private int capacity;
    //    保存数据元素的个数
    private int size;
    //    当数组大小不够的时候进行扩容，扩容的大小
    private int capacityincrement;

    //创建默认大小的栈
    public SequenceStack() {

        capacity = DEFAULT_SIZE;
        element = new Object[capacity];

    }

    //   创建带栈底的栈
    public SequenceStack(T t) {

        this();
        element[0] = t;
        size++;

    }

    public SequenceStack(T t, int initSize) {

        capacity = initSize;
        element = new Object[capacity];
        element[0] = t;
        size++;

    }

    public SequenceStack(T t, int initSize, int capacityincrement) {

        this(t, initSize);
        this.capacityincrement = capacityincrement;

    }

    //获取顺序表栈的大小

    public int length() {

        return size;
    }

    //   入栈

    public void push(T t) {

        ensureCapacity(size + 1);
        element[size] = t;
        size++;
    }

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    private void ensureCapacity(int mincapacity) {

        if (mincapacity > capacity) {

            if (capacityincrement > 0) {

                while (capacity < mincapacity) {

                    capacity += capacityincrement;
                }

            } else {

//                  capacityincrement 如果小于0，则让它*2的增长
                capacity <<= 1;

            }
            element = Arrays.copyOf(element, capacity);
        }

    }

    //    出栈
    public T pop() {

        T t = (T) element[size - 1];
        element[size - 1] = null;
        size--;
        return t;
    }

    //    返回栈顶元素，但不删除
    public T peek() {

        return (T) element[size - 1];
    }

    //判断栈是否为空
    public boolean empty() {

        return size == 0;
    }

    //    清空数据栈
    public void clear() {

        Arrays.fill(element, null);
        size = 0;
    }

}
